class Solution {
public:
    string numberToWords(int num) {
        if(!num) return "Zero";
        string res;
        int billion = num / 1000000000;
        num %= 1000000000;
        int million = num / 1000000;
        num %= 1000000;
        int thousand = num / 1000;
        num %= 1000;
        string interval = "";
        if(billion) 
        {
            res.append(hundreds_convert(billion) + " Billion");
            interval = " ";
        }
        if(million)
        {
            res.append(interval + hundreds_convert(million) + " Million");
            interval = " ";
        }
        if(thousand)
        {
            res.append(interval + hundreds_convert(thousand) + " Thousand");
            interval = " ";
        }
        if(num)
            res.append(interval + hundreds_convert(num));
        return res;
    }
    string ones_convert(int n) {
        switch(n) 
        {
            case 0:
                return "";
            case 1:
                return "One";
            case 2:
                return "Two";
            case 3:
                return "Three";
            case 4:
                return "Four";
            case 5:
                return "Five";
            case 6:
                return "Six";
            case 7:
                return "Seven";
            case 8:
                return "Eight";
            case 9:
                return "Nine";
        }
    }
    string tens_convert(int n) {
        if (n < 10) return ones_convert(n);
        if (n < 20)
        {
            switch(n) 
            {
                case 10:
                    return "Ten";
                case 11:
                    return "Eleven";
                case 12:
                    return "Twelve";
                case 13:
                    return "Thirteen";
                case 14:
                    return "Fourteen";
                case 15:
                    return "Fifteen";
                case 16:
                    return "Sixteen";
                case 17:
                    return "Seventeen";
                case 18:
                    return "Eighteen";
                case 19:
                    return "Nineteen";
            }
        }
        string res;
        switch(n / 10) 
        {
            case 2:
                res.append("Twenty");
                break;
            case 3:
                res.append("Thirty");
                break;
            case 4:
                res.append("Forty");
                break;
            case 5:
                res.append("Fifty");
                break;
            case 6:
                res.append("Sixty");
                break;
            case 7:
                res.append("Seventy");
                break;
            case 8:
                res.append("Eighty");
                break;
            case 9:
                res.append("Ninety");
                break;
        }
        if(n % 10) res.append(" " + ones_convert(n % 10));
        return res;
    }
    string hundreds_convert(int n) {
        string res = "";
        if(n / 100)
        {
            res.append(ones_convert(n / 100) + " Hundred");
            if(n % 100 == 0)
                return res;
            res.append(" ");
        }
        res.append(tens_convert(n % 100));
        return res;
    }
};
